﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace TSCC.WinControl
{
    public partial class ucCreateHtmlTable : UserControl
    {
        [Category("TSCC")]
        [Description("設定轉出時的檔案路徑"), DefaultValue(@"C:\")]
        public string XLSPath
        {
            get;set;
        }

        [Category("TSCC")]
        [Description("設定轉出時的檔案路徑"), DefaultValue(@"ABC.XLS")]
        public string XLSFileName
        {
            get;
            set;
        }

        StringBuilder tableBuilder = null;
        string tdTag = "<td style='border-width:1px;border-style:solid;'>{0}</td>";
        string tdColSpanTag = "<td style='border-width:1px;border-style:solid;' colspan='{1}'>{0}</td>";
        public ucCreateHtmlTable()
        {
            InitializeComponent();
        }

        public void CreateHtmlTable(DataTable p_dt, int p_pos, int p_repeatCol)
        {
            string strColCaption;
            tableBuilder = new StringBuilder();
            tableBuilder.Append(getNewLineString("<meta http-equiv=Content-Type content=text/html;charset=utf-8>"));
            tableBuilder.Append(getNewLineString("<table>"));

            StringBuilder bodyBuilder = new StringBuilder();
            strColCaption = createColumn(p_dt);
            if (p_pos > 0)
            {
                tableBuilder.Append(createFirstRow(p_dt, p_pos, p_repeatCol));
            }
            
            tableBuilder.Append(strColCaption);
            foreach(DataRow row in p_dt.Rows)
            {
                bodyBuilder.Append(getNewLineString("<tr>"));
                for (int i = 0; i < p_dt.Columns.Count; i++)
                {
                    bodyBuilder.AppendFormat(getNewLineString(tdTag)
                        , row[i].ToString());
                }
                bodyBuilder.Append(getNewLineString("</tr>"));
            }
            tableBuilder.Append(bodyBuilder.ToString());
            tableBuilder.Append(getNewLineString("</table>"));
            using (StreamWriter outfile = new StreamWriter(getFilePath()))
            {
                outfile.Write(tableBuilder.ToString());
            }
        }


        protected string getFilePath()
        {
            return XLSPath + XLSFileName;
        }

        protected string createColumn(DataTable p_dt)
        {
            StringBuilder colBuilder = new StringBuilder();
            colBuilder.Append(getNewLineString("<tr>") );
            foreach (DataColumn col in p_dt.Columns)
            {
                colBuilder.AppendFormat(getNewLineString(tdTag), col.Caption);
            }
            

            colBuilder.Append(getNewLineString("</tr>"));
            return colBuilder.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_dt"></param>
        /// <param name="emptyCol">決定前面要空多少個</param>
        /// <returns></returns>
        protected string createFirstRow(DataTable p_dt, int emptyCol, int repeatColCount)
        {
            StringBuilder colBuilder = new StringBuilder();
            string colName;
            colBuilder.Append(getNewLineString("<tr>"));
            //留空白，不加入
            for (int i = 0; i < emptyCol; i++)
            {
                colBuilder.AppendFormat(tdTag, string.Empty);                    
            }

            int x = emptyCol;

            while (x < p_dt.Columns.Count )
            {
                colName = p_dt.Columns[x].ColumnName.Replace("_1","");
                colBuilder.AppendFormat(tdColSpanTag, colName
                    , repeatColCount.ToString());
                x = x + repeatColCount;
            }


            colBuilder.Append(getNewLineString("</tr>"));
            return colBuilder.ToString();
        }

        protected string getNewLineString(string p_str)
        {
            return p_str + Environment.NewLine;
        }
    }
}
